package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.stronghold.*;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdNpcConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;

public class CrossStrongholdPb {
    public static StrongholdWarProto.StrongholdDispatchPatronsTempMsg.Builder getWildUserMsg(StrongholdWildUserData userData) {
        StrongholdWarProto.StrongholdDispatchPatronsTempMsg.Builder patronsMsg = StrongholdWarProto.StrongholdDispatchPatronsTempMsg.newBuilder();
        UserPatrons patrons = userData.getPatrons();
        patronsMsg.setPatronsId(patrons.getPatronsId());
        int curBlood = CrossStrongholdActivityMgr.calcCurBlood(patrons.getCurrentBlood(), patrons.getTotalBlood());
        patronsMsg.setCurBlood(curBlood);
        patronsMsg.setRewardPoints(userData.getRewardPoints());
        patronsMsg.setStrongholdId(userData.getWildId());
        return patronsMsg;
    }

    public static StrongholdWarProto.WildNpcEventMsg.Builder getNpcEventMsg(StrongholdNpcConfig npcConfig) {
        StrongholdWarProto.WildNpcEventMsg.Builder npcEventMsg = StrongholdWarProto.WildNpcEventMsg.newBuilder();
        npcEventMsg.setNpcId(npcConfig.getAppearanceId());
        npcEventMsg.setReward(npcConfig.getRewards());
        npcEventMsg.setConsume(npcConfig.getNeedMoney());
        npcEventMsg.setPoint(Integer.parseInt(npcConfig.getRewards().split(";")[0]));
        return npcEventMsg;
    }

    public static StrongholdWarProto.WildPatronsEventMsg.Builder getPatronsEventMsg(StrongholdPatronsEvent patronsEvent) {
        StrongholdWarProto.WildPatronsEventMsg.Builder patronsEventMsg = StrongholdWarProto.WildPatronsEventMsg.newBuilder();
        StrongholdWarProto.WildEnemyMsg.Builder enemyMsg = StrongholdWarProto.WildEnemyMsg.newBuilder();
        enemyMsg.setBlood(patronsEvent.getBlood());
        enemyMsg.setRewardPoints(patronsEvent.getRewardPoints());
        for (Integer boxType : patronsEvent.getBoxList()) {
            enemyMsg.addBox(boxType);
        }
        enemyMsg.setPatrons(PatronsPb.parsePatronsTempMsg(patronsEvent.getPatrons()));
        enemyMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(patronsEvent.getUserBaseInfo()));
        patronsEventMsg.setEnemy(enemyMsg);
        patronsEventMsg.setWinRewarad(patronsEvent.getWinReward());
        patronsEventMsg.setLoseReward(patronsEvent.getLoseReward());
        patronsEventMsg.setLosePoints(patronsEvent.getLosePoints());
        return patronsEventMsg;
    }

    public static StrongholdWarProto.WildBoxEventMsg.Builder getBoxEventMsg(StrongholdBoxEvent boxEvent) {
        StrongholdWarProto.WildBoxEventMsg.Builder boxEventMsg = StrongholdWarProto.WildBoxEventMsg.newBuilder();
        boxEventMsg.setBoxType(boxEvent.getBoxType());
        boxEventMsg.setReward(boxEvent.getReward());
        boxEventMsg.setPoint(boxEvent.getPoints());
        return boxEventMsg;
    }


    public static StrongholdWarProto.StrongholdEventTempMsg.Builder getEventMsg(int activityId,StrongholdBaseEvent eventData) {
        StrongholdWarProto.StrongholdEventTempMsg.Builder eventMsg = StrongholdWarProto.StrongholdEventTempMsg.newBuilder();
        int type = eventData.getType();
        if (type == 0) {
            //宝箱
            StrongholdBoxEvent boxEvent = (StrongholdBoxEvent) eventData;
            StrongholdWarProto.WildBoxEventMsg.Builder boxEventMsg = getBoxEventMsg(boxEvent);
            eventMsg.setBoxEvent(boxEventMsg);
        } else if (type == 1) {
            //npc
            StrongholdNpcEvent npcEvent = (StrongholdNpcEvent) eventData;
            StrongholdNpcConfig npcConfig = CrossStrongholdActivityMgr.getNpcConfig(activityId, npcEvent.getNpcId());
            StrongholdWarProto.WildNpcEventMsg.Builder npcEventMsg = getNpcEventMsg(npcConfig);
            eventMsg.setNpcEvent(npcEventMsg);
        } else if (type == 2) {
            //门客
            StrongholdPatronsEvent patronsEvent = (StrongholdPatronsEvent) eventData;
            StrongholdWarProto.WildPatronsEventMsg.Builder patronsEventMsg = getPatronsEventMsg(patronsEvent);
            eventMsg.setPatronsEvent(patronsEventMsg);
        }
        eventMsg.setType(type);
        return eventMsg;
    }

    public static StrongholdWarProto.StrongholdBoxDataTempMsg.Builder getBoxMsg(StrongholdWildBox box) {
        StrongholdWarProto.StrongholdBoxDataTempMsg.Builder boxMsg = StrongholdWarProto.StrongholdBoxDataTempMsg.newBuilder();
        boxMsg.setId(box.getBoxId());
        boxMsg.setType(box.getType());
        int cdTime = 0;
        if (box.getBoxId() != 0) {
            cdTime   =  CrossStrongholdActivityMgr.getBoxCdTime(box.getActivityId(), box.getType());
        }
        boxMsg.setOpenTime(box.getPickUpTime() + cdTime);
        return boxMsg;
    }

    public static  StrongholdWarProto.EliminateChallengePatronsDataMsg.Builder  parseEliminateChallengePatronsMsg(CrossStrongholdEliminateDefendPatrons defendPatrons) {
        StrongholdWarProto.EliminateChallengePatronsDataMsg.Builder msg = StrongholdWarProto.EliminateChallengePatronsDataMsg.newBuilder();
        msg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(defendPatrons.getUserBaseInfo()));
        msg.setPatrons(PatronsPb.parsePatronsTempMsg(defendPatrons.getPatrons()));
        msg.setPatronsBlood(CrossStrongholdActivityMgr.calcEliminateCurBlood(defendPatrons.getCurBlood(), defendPatrons.getTotalBlood()));
        return msg;
    }

    public static StrongholdWarProto.EliminateUnionBaseDataMsg.Builder parseEliminateUnionMsg(CrossStrongholdEliminateUnion union, int rank) {
        StrongholdWarProto.EliminateUnionBaseDataMsg.Builder msg = StrongholdWarProto.EliminateUnionBaseDataMsg.newBuilder();
        msg.setId(union.getId());
        msg.setRank(union.getId());
        msg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(union.getUnionBaseInfo()));
        msg.setStatus(union.getStatus());
        msg.setUnionId(union.getUnionUid());
        msg.setStar(union.getStar());
        msg.setRank(rank);
        msg.setBlood(CrossStrongholdActivityMgr.calcEliminateCurBlood(union.getCurBlood(), union.getTotalBlood()));
        return msg;
    }

    public static StrongholdWarProto.StrongholdNoticeMammonHelpMsg.Builder parseMammonHelpNoticeMsg(UserBaseInfo userBaseInfo,int mammonType,long userId) {
        StrongholdWarProto.StrongholdNoticeMammonHelpMsg.Builder msg = StrongholdWarProto.StrongholdNoticeMammonHelpMsg.newBuilder();
        msg.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.setType(mammonType);
        msg.setUserId(userId);
        return msg;
    }

    public static PeakStrongholdWarProto.PeakStrongholdNoticeMammonHelpMsg.Builder parsePeakMammonHelpNoticeMsg(UserBaseInfo userBaseInfo, int mammonType, long userId) {
        PeakStrongholdWarProto.PeakStrongholdNoticeMammonHelpMsg.Builder msg = PeakStrongholdWarProto.PeakStrongholdNoticeMammonHelpMsg.newBuilder();
        msg.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.setType(mammonType);
        msg.setUserId(userId);
        return msg;
    }
}
